package com.spark.core.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * 取出RDD中的前N个元素
 */
object Demo2_take {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("take").setMaster("local")
    val sc = new SparkContext(conf)
    sc.setLogLevel("error")

    // 加载目录下的所有文件
    val lines = sc.textFile("data/words")

    println(lines.getNumPartitions)

    // 排序取topN
    val wordCount: RDD[(String, Int)] = lines.flatMap(_.split(",")).map((_, 1)).reduceByKey(_ + _)

    wordCount.foreach(println)

    println("*" * 100)
    // val result: Array[(String, Int)] = wordCount.sortBy(kv => kv._2, true).take(2)
    // result.foreach(println)
    val result: (String, Int) = wordCount.sortBy(kv => kv._2, true).first()
    println(result)


  }
}
